#ifndef __PLATFORM_LIB_TREE_H__
#define __PLATFORM_LIB_TREE_H__
#ifdef __cplusplus
extern "C"
{
#endif

#include "pf_common.h" 

/******************************************************************************************************/

typedef struct pf_tree_node pf_tree_node_t;

typedef struct pf_tree pf_tree_t;

/*********************************************************************************************
Function Name:  pf_tree_create
Description  :  Create a rb tree
Inputs       :  int (*comp)(const void*, const void*)    :  The Compare function
Outputs      :  return value                             :  The tree created
ErrorCodes   :  PF_ENOMEM                                :  Out of memory
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_tree_t* pf_tree_create(int (*comp)(const void*, const void*));

/*********************************************************************************************
Function Name:  pf_tree_destory
Description  :  Destory a rb tree
Inputs       :  pf_tree_t* tree                     :  The rb tree's pointer
Outputs      :  return value                             :  Yes or no to destory
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_tree_destory(pf_tree_t* tree);

/*********************************************************************************************
Function Name:  pf_tree_get_value
Description  :  Get value from a node
Inputs       :  pf_tree_t_node* tree                :  The given tree node
Outputs      :  return value                             :  The value of the given node
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern void* pf_tree_get_value(const pf_tree_node_t* node);

/*********************************************************************************************
Function Name:  pf_tree_head
Description  :  Get head node of the tree
Inputs       :  pf_tree_t* tree                     :  The rb tree
Outputs      :  return value                             :  The head node of the tree
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_tree_node_t* pf_tree_head(const pf_tree_t* tree);

/*********************************************************************************************
Function Name:  pf_tree_tail
Description  :  Get tail node of the tree
Inputs       :  pf_tree_t* tree                     :  The rb tree
Outputs      :  return value                             :  The tail node of the tree
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_tree_node_t* pf_tree_tail(const pf_tree_t* tree);

/*********************************************************************************************
Function Name:  pf_tree_next
Description  :  Get next node of the node
Inputs       :  pf_tree_t_node* cur_node            :  The rb tree's one node
Outputs      :  return value                             :  The next node of the given node
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_tree_node_t* pf_tree_next(const pf_tree_node_t* cur_node);

/*********************************************************************************************
Function Name:  pf_tree_prev
Description  :  Get prev node of the node
Inputs       :  pf_tree_t_node* cur_node            :  The rb tree's one node
Outputs      :  return value                             :  The prev node of the given node
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_tree_node_t* pf_tree_prev(const pf_tree_node_t* cur_node);

/*********************************************************************************************
Function Name:  pf_tree_find
Description  :  Find a node match the value
Inputs       :  pf_tree_t_node* cur_node            :  The rb tree's one node
                void* value                              :  The value need to find
Outputs      :  return value                             :  The found node
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_tree_node_t* pf_tree_find(const pf_tree_t* tree, const void* value);

/*********************************************************************************************
Function Name:  pf_tree_find_less
Description  :  Find the maxmum less node of the value
Inputs       :  pf_tree_t_node* cur_node            :  The rb tree's one node
                void* value                              :  The value need to find
                int can_equal                            :  1:can be equal
                                                         :  0:not can be equal
Outputs      :  return value                             :  The found node
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_tree_node_t* pf_tree_find_less(const pf_tree_t* tree, 
                                         const void* value, 
                                         int can_equal);

/*********************************************************************************************
Function Name:  pf_tree_insert
Description  :  Insert a node into the rb tree
Inputs       :  pf_tree_t* tree                     :  The rb tree
                void* value                              :  The value need to insert
Outputs      :  return value                             :  Success or fail to insert
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_tree_insert(pf_tree_t* tree, void* value);

/*********************************************************************************************
Function Name:  pf_tree_erase
Description  :  Erase a specify node and return it's value
Inputs       :  pf_tree_t* tree                     :  The rb tree
                pf_tree_t_node* node                :  The rb tree's one node
Outputs      :  return value                             :  The data of the erased node
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern void* pf_tree_erase(pf_tree_t* tree, pf_tree_node_t* node);

/*********************************************************************************************
Function Name:  pf_tree_erase_value
Description  :  Erase the node which match the value
Inputs       :  pf_tree_t* tree                     :  The rb tree
                void* value                              :  The value which need to erase
Outputs      :  return value                             :  Success or fail to erase
ErrorCodes   :  PF_ENODATA                               :  No data available
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_tree_erase_value(pf_tree_t* tree, const void* value);

/*********************************************************************************************
Function Name:  pf_tree_count
Description  :  Get node count of a tree
Inputs       :  pf_tree_t* tree                     :  The rb tree
Outputs      :  return value                             :  The number of all nodes
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern uint32_t pf_tree_count(const pf_tree_t* tree);

/*********************************************************************************************
Function Name:  pf_tree_clear
Description  :  Erase all nodes of a tree
Inputs       :  pf_tree_t* tree                     :  The rb tree
Outputs      :  return value                             :  Success or fail to clear
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_tree_clear(pf_tree_t* tree);

/*********************************************************************************************
Function Name:  pf_tree_for_each
Description  :  Erase all nodes of a tree
Inputs       :  pf_tree_t* tree                     :  The rb tree
                int forward                              :  1:from head to tail;
                                                            0:from tail to head
                void* arg                                :  The callback function 's param
                int  (*cb)(void* arg, void* value)       :  The callback function when traverse
Outputs      :  return value                             :  Success or fail to for_each
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int32_t pf_tree_for_each(const pf_tree_t* tree,
                                int forward,
                                void* arg,
                                int (*cb)(void* arg, pf_tree_node_t* node));

#if 0
/* serialization (maybe later) */
extern int32_t pf_tree_serialize(const pf_tree_t* tree,
                                 char* buffer,
                                 int32_t size);

extern pf_tree_t* pf_tree_deserialize(const char* data);
#endif

/******************************************************************************************************/

#ifdef __cplusplus
}
#endif
#endif /* #ifndef __PLATFORM_LIB_TREE_H__ */


